---
title: "Reconsidering the Costs of Commitment_Replication Code"
author: "Andreas J. Ullmann"
date: "2024-04-25"
output: html_document
---
# 1. Data Preparation
## Uploading the Basic Data Set

```{r setup, echo=TRUE, warning=FALSE, message=FALSE}
library(survival)
library(car)
library(reshape)
library(survminer)
library(stargazer)
library(corrplot)
library(Hmisc)
library(simPH)
library(here)
library(dplyr)
library(countrycode)
library(coxme)

SURV0 <- read.csv(here::here("data", "Commitment Dataset.csv"),header=T, dec=".", sep = ";")

```
## Create Summary Statistics Table and Correlation Matrix
```{r, echo=TRUE, warning=FALSE, message=FALSE}
library(vtable)

SURV0$law.system <- admisc::recode(SURV0$LAW.SYSTEM, "'CIVIL'=1;'MIXED'=2;'COMMON'=3;'MUSLIM'=0;else=0")

X <- cbind(SURV0$ADVERSE.VIEWS.COUNTRY2, SURV0$ADVERSE.VIEWS.COUNTRY.5YEAR,SURV0$ADVERSE.VIEWS.REGION, SURV0$ADVERSE.VIEWS.REGORG, SURV0$ADVERSE.VIEWS.REGIME.YEAR,SURV0$NUMBER.ADV.VIEWS,  SURV0$v2x_polyarchy, SURV0$Fariss.Score, SURV0$law.system, SURV0$v2csreprss, SURV0$RATRATE.REGORG.CCPR, SURV0$CAPABILITY, SURV0$Compliance.Yearly.Mean, SURV0$reg_trans)
colnames(X) = c("Views per Country","Views per Country, past 5 years", "Views per Region","Views per Regional Organization", "Views per Regime Type","Views Globally", "Electoral Democracy","Physical Integrity Rights","Law System","Civil Society Freedom","Regional Ratification Rate CCPR-OP", "GDP per capita","Compliance Score", "Regime Change")
xdata <- as.data.frame(X)
sumtable(xdata, summ = c('notNA(x)', 'mean(x)', 'sd(x)', 'min(x)','max(x)'))

#create correlation matrix
corrplot(cor(X, use = "complete.obs"),method = 'number', order = 'AOE', tl.col = "black", tl.cex = 1, cl.cex = 1, number.cex = 0.9)

```


## Preparing the Data Set for Analysis CAT Article 22
```{r,warning = FALSE, results="hide", message=FALSE, echo=TRUE}
#let the observation end for each country the year after CAT Art. 22 acceptance
SURV0 <- SURV0 %>%
  arrange(COUNTRY, YEAR)
SURV0 <- SURV0 %>%
  group_by(COUNTRY) %>%
  filter(!any(CAT.Art..22 == 1) | row_number() <= which.max(CAT.Art..22 == 1)) %>%
  ungroup()

#let each country observation start the year the CAT was ratified
SURV0 <- subset(SURV0, !CAT==0)

#add year count
SURV0 <- SURV0 %>%
  group_by(COUNTRY) %>%
  mutate(time = row_number() - 1)

#cut the vectors
start.cat <- as.numeric(SURV0$time)
stop.cat <- as.numeric(SURV0$time+1)
caticp <- as.numeric(SURV0$CAT.Art..22)
country.cat <- as.factor(SURV0$COUNTRY)

views.ind.lag <- Lag(as.numeric(SURV0$ADVERSE.VIEWS.COUNTRY2, na.rm=T), -1)
views.all.lag <- Lag(as.numeric(SURV0$NUMBER.ADV.VIEWS+SURV0$ADVERSE.VIEWS.COUNTRY2, na.rm=T), -1)
views.region.lag <- Lag(views.region.cat <- as.numeric(SURV0$ADVERSE.VIEWS.REGION - SURV0$ADVERSE.VIEWS.COUNTRY2), -1)
views.regorg.lag <- Lag(as.numeric(SURV0$ADVERSE.VIEWS.REGORG - SURV0$ADVERSE.VIEWS.COUNTRY2), -1)
views.regime.year <- as.numeric(Lag(SURV0$ADVERSE.VIEWS.REGIME.YEAR, -1))
views.ind.3 <- as.numeric(Lag(SURV0$ADVERSE.VIEWS.COUNTRY.3YEAR, -1))
views.ind.5 <- as.numeric(Lag(SURV0$ADVERSE.VIEWS.COUNTRY.5YEAR, -1))
views.gen <- as.numeric(Lag(SURV0$ADVERSE.VIEWS.COUNTRY.YEAR.GENERALONLY.CUM, -1))
views.gen.add <- as.numeric(views.ind.lag+views.gen)
views.uncon <- as.numeric(Lag(SURV0$ADVERSE.VIEWS.COUNTRY.YEAR.UNCONDITIONAL.CUM, -1))
sensitive <- as.numeric(Lag(SURV0$ADVERSE.VIEWS.SENS.COUNTRY.YEAR,-1))
sensitive.cum <- as.numeric(Lag(SURV0$ADVERSE.VIEWS.SENS.CUM,-1))

complier.change <- as.numeric(Lag(SURV0$Compliance.Yearly.Mean), -1)
SURV0$complier.change.all <- admisc::recode(SURV0$Compliance.Yearly.Mean, "NA=0; else='copy'")
complier.change.all <- as.numeric(SURV0$complier.change.all)
SURV0$rateregion <- as.double(SURV0$CAT.RATRATE.REGION, na.rm = T)
rateregion <- as.numeric(SURV0$rateregion)
rateregion.lag <- Lag(rateregion-2)
SURV0$rateregorg <- as.double(SURV0$RATRATE.REGORG.CAT, na.rm = T)
rateregorg.lag <- Lag(SURV0$rateregorg-2)
hr.fariss <- as.numeric(SURV0$Fariss.Score)
polityiv <- as.numeric(SURV0$DEMOCRACY)
vdem.lib <- as.double(SURV0$v2x_libdem)
vdem.egal <- as.numeric(SURV0$v2x_egaldem)
vdem.poly <- as.numeric(SURV0$v2x_polyarchy)
regime <- as.numeric(admisc::recode(SURV0$v2x_polyarchy,"0:0.49=0; 0.5:0.913=1"))
regime_type <- as.numeric(SURV0$reg_type)
SURV0$lnGDP <- as.numeric(log(SURV0$CAPABILITY))
GDP <- as.numeric(SURV0$lnGDP, na.rm=T)
SURV0$law.system <- admisc::recode(SURV0$LAW.SYSTEM, "'CIVIL'=1;'MIXED'=2;'COMMON'=3;'MUSLIM'=NA;'CUSTOMARY'=NA; ''=NA")
common <- admisc::recode(SURV0$LAW.SYSTEM, "3=1; 1:2=0")
common <- as.numeric(common)
csrprss <- as.numeric(SURV0$v2csreprss)
regime.change <- as.numeric(Lag(SURV0$reg_trans, -1))

```

# 2. Cox Regressions
## Checking Schoenfeld Residuals and Variance Inflation Factor
```{r, echo=TRUE,  warning=FALSE, message=FALSE}
fit.schoen <- coxph(Surv(start.cat, stop.cat, caticp) ~ views.ind.lag +  vdem.poly + hr.fariss + GDP + law.system + rateregorg.lag + regime.change, data = SURV0, cluster = country)
schoen <- cox.zph(fit.schoen)
schoen

vif(fit.schoen)
```

## Main Regression Models
**Views per Group Variables**
```{r, echo=TRUE,  warning=FALSE, message=FALSE}
#episode splitting
survobject.cat <- Surv(start.cat, stop.cat, caticp)

#Views per group variables

cat1.1 <- coxph(survobject.cat ~ views.all.lag + tt(views.all.lag) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat1.1)
cat1.2 <- coxph(survobject.cat ~ views.region.lag + tt(views.region.lag) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat1.2)
cat1.3 <- coxph(survobject.cat ~ views.regorg.lag + tt(views.regorg.lag) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat1.3)
cat1.4 <- coxph(survobject.cat ~ views.regime.year + tt(views.regime.year) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat1.4)

```
**Views Per Country Variables**
```{r, echo=TRUE,  warning=FALSE, message=FALSE}
cat2.1 <- coxph(survobject.cat ~ views.ind.lag + tt(views.ind.lag) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat2.1)
cat2.2 <- coxph(survobject.cat ~ views.ind.3 + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat2.2)
cat2.3 <- coxph(survobject.cat ~ views.ind.5 + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat2.3)
cat2.4 <- coxph(survobject.cat ~ views.gen+ tt(views.gen) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat2.4)
cat2.5 <- coxph(survobject.cat ~ views.ind.lag*complier.change.all + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat2.5)
```
**Interactions and Alternative Measures**
```{r, echo=TRUE,  warning=FALSE, message=FALSE}
cat3.1 <- coxph(survobject.cat ~ views.regorg.lag*regime + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat3.1)
cat3.2 <- coxph(survobject.cat ~ views.regorg.lag*complier.change.all + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat3.2)
cat3.3 <- coxph(survobject.cat ~ views.region.lag*hr.fariss + vdem.poly + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat3.3)
cat3.4 <- coxph(survobject.cat ~ views.uncon + tt(views.uncon) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat3.4)
cat3.5 <- coxph(survobject.cat ~ sensitive + tt(sensitive) + vdem.poly + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat3.5)
cat3.6 <- coxph(survobject.cat ~ views.ind.lag + tt(views.ind.lag) + polityiv + hr.fariss + law.system + GDP + rateregorg.lag + tt(rateregorg.lag) + regime.change, data = SURV0, cluster = COUNTRY)
summary(cat3.6)
```
## Marginal Effects Plots of Interactions

**Plot for Figure 1**
```{r, echo=TRUE,  warning=FALSE, message=FALSE}
#set font
windowsFonts(TNR=windowsFont("Times New Roman"))

#plot for Figure 1
CAT2.1 <- coxph(Surv(start.cat, stop.cat, caticp) ~ views.regorg.lag*regime)
CATSim2.1 <- coxsimInteract(CAT2.1, b1 = "views.regorg.lag", b2 = "regime", qi = "Marginal Effect",  X2 = c(0,1), ci=0.9)
margin.cat2.1 <- simGG(CATSim2.1, type = "points", alpha = 0.1, xlab = "Regime Type",
                        ylab = "Marginal Effect of Views/Regional Organization", title = "CAT Article 22", pcolour = "#000000", lcolour = "#FFFFFF", method = "loess")+
  theme(text=element_text(family="TNR", face="bold", size=12, colour = "black"))+
  theme(axis.title = element_text(size = 11))
margin.cat2.1 <- margin.cat2.1 + scale_x_continuous(breaks = seq(0, 1, by = 1),labels=c("AUT", "DEM")) + 
  ylim(0.90,1.05) +
  geom_jitter(width = 0.02, height = 0.01, shape=20,  alpha = 0.1)
margin.cat2.1
```
**Plot for Figure 3**
```{r, echo=TRUE, warning=FALSE, message=FALSE}
CAT1 <- coxph(Surv(start.cat, stop.cat, caticp) ~ views.ind.lag*complier.change.all)
CATSim1 <- coxsimInteract(CAT1, b1 = "views.ind.lag", b2 = "complier.change.all", qi = "Marginal Effect",  X2 = seq(0, 1, by = 0.02), spin = TRUE)
margin.cat1 <- simGG(CATSim1, type = "points", alpha = 0.1, xlab = "Compliance Score",
      ylab = "Marginal effect of Views/Country", title = "CAT Article 22", rug=T, lcolour = "#000000", pcolour = "#000000", method="loess") +
  theme(text=element_text(family="TNR", face="bold", size=12, colour = "black"))+
  theme(axis.title = element_text(size = 10))
margin.cat1 <- margin.cat1 + scale_x_continuous(breaks = seq(0, 1, by = .25)) + scale_y_continuous(breaks = seq(0, 1, by = 0.1)) + ylim(0,1)
margin.cat1
```
